﻿// 功能：弹出式对话框按钮集合
// 描述：
// 作者：温伟鹏
// 日期：2010/9/10 14:58:07

using System;
using System.Collections;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 弹出式对话框按钮集合
    /// </summary>
    public class PopupDialogButtonCollection:CollectionBase
    {
        /// <summary>
        /// 集合中第一个按钮的Id
        /// </summary>
        private static uint FirstButtonId;

        /// <summary>
        /// 获取集合的所有者
        /// </summary>
        public PopupDialog Owner
        {
            get;
            private set;
        }
        /// <summary>
        /// 根据索引获取集合中的按钮实例
        /// </summary>
        /// <param name="index">按钮在集合中的索引</param>
        /// <returns>返回null，表示未找到；</returns>
        public PopupDialogButton this[int index]
        {
            get{
                return (PopupDialogButton)InnerList[index];
            }
        }

        /// <summary>
        /// 构造按钮集合实例
        /// </summary>
        /// <param name="owner">按钮集合所有者</param>
        public PopupDialogButtonCollection(PopupDialog owner)
        {
            this.Owner = owner;
        }

        /// <summary>
        /// 生成新的按钮Id
        /// </summary>
        /// <returns></returns>
        private uint GenerateNewId()
        {
            lock(this)
            {
                return FirstButtonId++;
            }
        }
        /// <summary>
        /// 重新设置集合中按钮的Id
        /// </summary>
        internal void ResetButtonsId()
        {
            lock(this)
            {
                FirstButtonId = PopupDialog.FirstButtonId;

                for (int i = 0; i < Count;i++ )
                {
                    this[i].Id = GenerateNewId();
                }
            }
        }
        /// <summary>
        /// 向集合中添加按钮
        /// </summary>
        /// <param name="button">待添加的按钮</param>
        public void Add(PopupDialogButton button)
        {
            button.Owner = this.Owner;

            InnerList.Add(button);
        }
        /// <summary>
        /// 根据按钮Id从集合中获取按钮实例
        /// </summary>
        /// <param name="buttonId">按钮Id</param>
        /// <returns>返回null，表示未找到按钮</returns>
        public PopupDialogButton GetButtonById(uint buttonId)
        {
            if (buttonId < PopupDialog.FirstButtonId)
                return null;

            lock(this)
            {
                for(int i = 0;i<InnerList.Count;i++)
                {
                    PopupDialogButton btn = this[i];

                    if (btn.Id == buttonId)
                        return btn;
                }

                return null;
            }
        }
        /// <summary>
        /// 清空按钮集合
        /// </summary>
        public new void Clear()
        {
            InnerList.Clear();

            try
            {
                this.Owner.RemoveAllUserButtons();
            }
            catch (Exception)
            {}
        }
    }
}
